using System;
using System.Drawing;
using System.ComponentModel;

namespace Camalon.Drawing
{
	[Serializable]
	[TypeConverter(typeof(RectangleMarginsConverter))]
	public struct RectangleMargins
	{
		private int _top;
		private int _bottom;
		private int _left;
		private int _right;

		public RectangleMargins(int left, int right, int top, int bottom )
		{
			this._top	= top;
			this._left	= left;
			this._right	= right;
			this._bottom	= bottom;
		}

		public RectangleMargins( Rectangle root, Rectangle child )
		{
			this._top		= child.Top - root.Top;
			this._left		= child.Left - root.Left;
			this._right		= root.Right - child.Right;
			this._bottom	= root.Bottom - child.Bottom;
	
		}

		public RectangleMargins( RectangleF root, RectangleF child )
			:this( Rectangle.Round( root), Rectangle.Round(child)){}

		public static RectangleMargins Empty
		{	
			get{ return new RectangleMargins(0,0,0,0);}		
		}

		public bool IsEqual( RectangleMargins margins)
		{
			return 
				this._top == margins._top &&
				this._left == margins._left&&
				this._right == margins._right &&
				this._bottom == margins._bottom;
		}

		public void SetMargins( Rectangle root, Rectangle child )
		{
			this._top	= child.Top - root.Top;
			this._left	= child.Left - root.Left;
			this._right	= root.Right - child.Right;
			this._bottom = root.Bottom - child.Bottom;
	
		}

		[Browsable( false)]
		public bool IsEmpty
		{
			get
			{
				return this.IsEqual( Empty );
			}
		}

		public Rectangle GetRootBounds( Rectangle child )
		{
			//Convert to root bounds
			child.X			-= this._left;
			child.Y			-= this._top;
			child.Width		+= this._right + this._left;
			child.Height	+= this._top + this._bottom;
			return child;
		}
		
		public RectangleF GetRootBounds( RectangleF child )
		{
			//Convert to root bounds
			child.X			-= this._left;
			child.Y			-= this._top;
			child.Width		+= this._right + this._left;
			child.Height	+= this._top + this._bottom;
			return child;
		}

		public Rectangle GetChildBounds( Rectangle root )
		{
			//Convert to child bounds
			root.X			+= this._left;
			root.Y			+= this._top;
			root.Width		-= this._right + this._left;
			root.Height		-= this._top + this._bottom;
			return root;
		}

		public RectangleF GetChildBounds( RectangleF root )
		{
			//Convert to child bounds
			root.X			+= this._left;
			root.Y			+= this._top;
			root.Width		-= this._right + this._left;
			root.Height		-= this._top + this._bottom;
			return root;
		}

		[DefaultValue(0)]
		public int Top
		{
			get{ return _top;}
			set{ _top = value;}
		}
		[DefaultValue(0)]
		public int Left
		{
			get{ return _left;}
			set{ _left = value;}
		}
		[DefaultValue(0)]
		public int Right
		{	
			get{ return _right;}
			set{ _right = value;}
		}
		[DefaultValue(0)]
		public int Bottom
		{
			get{ return _bottom;}
			set{ _bottom = value;}
		}
	}


}
